package edu.njust.cma.mapper;

import edu.njust.cma.pojo.Equipment;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface EquipmentMapper {
    //1.查询设备列表
    @Select("select * from equipment")
    List<Equipment> equipList();


    //2.添加设备
    @Insert("insert into equipment (name,equipment_number,measuring_range,description,purchase_time," +
            "expiry_time,purchase_method,calibration_certificate_url,operation_manual_url,authorized_users_list_url)" +
            " values (#{name},#{equipmentNumber},#{measuringRange},#{description},#{purchaseTime},#{expiryTime}" +
            ",#{purchaseMethod},#{calibrationCertificateUrl},#{operationManualUrl},#{authorizedUsersListUrl})")
    void addEquip(Equipment equipment);

    //3.删除设备
    @Delete("delete from equipment where id=#{id}")
    void deleteEquip(int id);

    //4.修改设备信息
//    @Update("update equipment set name=#{name},equipment_number=#{equipmentNumber},measuring_range=#{measuringRange}," +
//            "description=#{description},purchase_time=#{purchaseTime},expiry_time=#{expiryTime},purchase_method=#{purchaseMethod}," +
//            "calibration_certificate_url=#{calibrationCertificateUrl},operation_manual_url=#{operationManualUrl}," +
//                    "authorized_users_list_url=#{authorizedUsersListUrl} where id=#{id}")
    void updateEquip(Equipment equipment);

    //5.通过关键字查询设备
    @Select("select * from equipment where name like concat('%',#{keyword},'%')")
    List<Equipment> searchEquip(String keyword);

    @Select("select * from equipment where id=#{equipmentId}")
    Equipment getEquipment(int equipmentId);

    //检查subTask表中是否有正在使用该设备
    @Select("select id from subtasks_equipment where equipment_id=#{id}")
    List<Integer> getSubTaskEquipmentList(int id);

    //通过项目ID获取所有的standardId
    @Select("select standard_id from small_project where project_id=#{projectId}")
    List<Integer> getStandardIdListByProjectId(int projectId);

    @Select("select DISTINCT major_category_name from standards where id=#{standardId}")
    String getMajorCategoryNameByStandardId(int standardId);

    // 去重
    @Select("select  distinct minor_category_name  from standards where id=#{standardId}")
    String getMinorCategoryNameByStandardId(int standardId);

    @Select("select  name from tasks where small_project_id=#{smallProjectId}")
    List<String> getTaskNameBySmallProjectId(int smallProjectId);

    @Select("select DISTINCT name from standards where id=#{standardId}")
    String getStandardNameByStandardId(int standardId);

    @Select("select  DISTINCT number from standards where id=#{standardId}")
    String getStandardNumberByStandardId(int standardId);

    @Select("select id from small_project where project_id=#{projectId}")
    List<Integer> getSmallProjectIdListByProjectId(int projectId);

    @Select("select id from tasks where small_project_id=#{smallProjectId}")
    List<Integer> getTaskIdListBySmallProjectId(int smallProjectIdList);

    @Select("select equipment_id from subtasks_equipment where task_id=#{taskId}")
    List<Integer> getEquipmentIdListByTaskId(int taskId);

    @Select("select DISTINCT equipment_number from equipment where id=#{equipmentId}")
    String getEquipmentNumberByEquipmentId(int equipmentId);

    @Select("select DISTINCT name from equipment where id=#{equipmentId}")
    String getEquipmentNameByEquipmentId(int equipmentId);

    @Select("select DISTINCT expiry_time from equipment where id=#{equipmentId}")
    String getExpiryTimeByEquipmentId(int equipmentId);

    @Select("select DISTINCT measuring_range from equipment where id=#{equipmentId}")
    String getMeasuringRangeByEquipmentId(int equipmentId);

    @Select("select  DISTINCT description from equipment where id=#{equipmentId}")
    String getDescriptionByEquipmentId(int equipmentId);

    @Select("select DISTINCT traceway from equipment where id=#{equipmentId}")
    int getTraceWayByEquipmentId(int equipmentId);
}
